// -----------------------------------------------------------------------------
// all group 
// _playtype 参考 RateConfig_5h $this->playGroupArr 对应
// -----------------------------------------------------------------------------
var M = new Object();
M.Rates = new Array();
M.RatesTimeId = 0;
M.rateI = 0; 
$.fn.getAjaxRatedata = function(_lotto_id, _gametype, _playtype){
 if (M.RatesTimeId) { clearTimeout(M.RatesTimeId); }
 $().disAreaObj();
 // debug window.open('op.php?op=xml_3d&fp=rate_5h&playtype=' + _playtype + '&lotto_id_5h=' + _lotto_id + "&gametype=" + _gametype + "&disk="+ $("select#slcDisk").val() + "&xyid="+ $("select#xyidSlc").val() );
 $.getJSON('op.php?op=xml_3d&fp=rate_5h&playtype=' + _playtype + '&lotto_id_5h=' + _lotto_id + "&gametype=" + _gametype + "&disk="+ $("select#slcDisk").val() + "&xyid="+ $("select#xyidSlc").val() ,function(ratedata){
    var dataLen = 0;
    var urlPre = "";
    var hrefStr = "";

    if(ratedata == undefined){
      $("div#errormsg").addClass("error").text("系统维护中");
    }else if(ratedata.errormsg != undefined){
      $("div#errormsg").addClass("error").text("No Rate data");
    }else{
      var xyid = $("select#xyidSlc").val();
      var disk = $("select#slcDisk").val();
      if( xyid == undefined ){ xyid = 0; } 
      M.rateI = 0;                     
      //show table & ratedata
      //$().showTableData(ratedata,_lotto_id,_gametype,_playtype,xyid,disk);
      var htmStr="";
      switch(_playtype){
         case 'r1':
             htmStr = show_r1(ratedata,_lotto_id,_gametype,_playtype, xyid, disk); break;
         case 'rp1':
         case 'rp2':
         case 'rp3':
         case 'rp4':
         case 'rp5':
             htmStr = show_rpx(ratedata,_lotto_id,_gametype,_playtype, xyid, disk); break;
         default:
             htmStr = show_rxcx(ratedata,_lotto_id,_gametype,_playtype, xyid, disk);
      }  
      $("#myDivTbl").html(htmStr);
    }
    
   $().redisAreaObj();   
   $("div#mainPageData").show();
   $("div#errormsg").text("");
   // change text or checkbox type
    var timebtnType = $("input:hidden#timebtnType").val();
    if(timebtnType == "checkbox"){
      $().timebtnTypeToCheckbox();
      $('.tblMy3D input:checkbox').click(function(){
        var chkVal = $(this).attr('checked');
        if(chkVal){ $(this).addTheValClr(null, '') }
        else{ $(this).clrTheValClr(null); }
        computeUnion(); // count Group numbers
      });
    }else{
      $('.tblMy3D input:text').change(function(){
        var chkVal = $(this).val();
        if(chkVal != ''){ $(this).addTheValClr(null, '') }
        else{ $(this).clrTheValClr(null); }
      });
    }
    
    $("table.tblMy3D td:has(input)").parent("tr.td2").hover(
      function() {$(this).children().addClass("trover");}
      , function() {$(this).children().removeClass("trover");}
    );
    
    // check rate each 30 sec
    if( _timestap > 0 )
    {
      M.ReNewTime = Math.floor( ( Math.random() * 100 ) % 30 + 30 ) * 1000;
      M.RatesTimeId = setTimeout("$().getAjaxRatedata(" + _lotto_id + "," + _gametype + ",'" + _playtype + "');", M.ReNewTime);
    }else if ( M.RatesTimeId ) { clearTimeout(M.RatesTimeId); }  
    
  });
};

// show table data
// jason data format={"bXX":{"number":["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21"],
//                          "rates":[1.92,1.92,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00]},

// bettype=7,8
function show_r1(rdata,_lotto_id,_gametype,_playtype, xyid, disk){
  var sStr = '<table class="tblMy3D" style="table-layout:fixed; ">';
  var showBetSubArr = '';
  var htmlTitle = '<td>号码</td><td>赔率</td><td>金额</td>';
  var i = 0;
  var l = 0;
  var oShow = '';
  var showTitleArr03 = [['总单', '总双'], ['总大', '总小']];
  var showTitleArr05 = [['大', '小'], ['单', '双']];
  var showTitleArr06 = [['尾大', '尾小'], ['合单', '合双']];                
  var subidArr = '';
  var sTitle = '';
  var subidShow = '';
  var showBetSubArr = [
       [  '总和', [rdata.b7, rdata.b8], [7,8], [0, 0], showTitleArr03, 1, '']
      ,['平码一', [rdata.b2_1, rdata.b1_1], [2,1], [1, 1], showTitleArr05, 2, '']
      ,['平码二', [rdata.b2_2, rdata.b1_2], [2,1], [2, 2], showTitleArr05, 2, '']
      ,['平码三', [rdata.b2_3, rdata.b1_3], [2,1], [3, 3], showTitleArr05, 2, '']
      ,['平码四', [rdata.b2_4, rdata.b1_4], [2,1], [4, 4], showTitleArr05, 2, '']
      ,[  '特码', [rdata.b2_5, rdata.b1_5], [2,1], [5, 5], showTitleArr05, 2, '']
      ,['平码一', [rdata.b3_1, rdata.b4_1], [3,4], [1, 1], showTitleArr06, 1, '']
      ,['平码二', [rdata.b3_2, rdata.b4_2], [3,4], [2, 2], showTitleArr06, 1, '']
      ,['平码三', [rdata.b3_3, rdata.b4_3], [3,4], [3, 3], showTitleArr06, 1, '']
      ,['平码四', [rdata.b3_4, rdata.b4_4], [3,4], [4, 4], showTitleArr06, 1, '']
      ,[  '特码', [rdata.b3_5, rdata.b4_5], [3,4], [5, 5], showTitleArr06, 1, '']
  ];
  sStr += '<tr class="bgClc_' + _gametype   + '"><td>类别</td>' + htmlTitle+ htmlTitle+ htmlTitle+ htmlTitle + '</tr>';
  for(i = 0, l = showBetSubArr.length; i < l; i++){
    oShow =  showBetSubArr[i];
    if( i == 1) { sStr += '<tr class="bgClc_' + _gametype   + '"><td>类别</td>' + htmlTitle+ htmlTitle+ htmlTitle+ htmlTitle + '</tr>'; }
    sStr += crt_2face_com(oShow[1], oShow[2], oShow[3], oShow[0], oShow[4], oShow[5], 4, disk, _playtype, oShow[6], xyid);
  }
  
  sStr += '</table>';
  return sStr;
}

function show_rpx(rdata,_lotto_id,_gametype,_playtype, xyid, disk){
  var sStr = '<table class="tblMy3D" width="700">';
  var showBetSubArr = '';
  var i = 0;
  var l = 0;
  var oShow = '';
  var showTitleArr = [['单', '双'], ['大', '小']];       
  var showTitleArr02 = [['合单', '合双'], ['尾大', '尾小']];
  var showTitleArr03 = [['总单', '总双'], ['总大', '总小']];
  var showTitleArr04 = [['福', '禄', '寿', '喜']];
  var showTitleArr05 = [['红', '蓝', '绿']];
  var subidArr = '';
  var sTitle = '';
  var subidShow = '';
  switch(_playtype){
    case 'rp1':
      sTitle = '平码一';
      subidShow = 1;
      break;
    case 'rp2':
      sTitle = '平码二';
      subidShow = 2;
      break;
    case 'rp3':
      sTitle = '平码三';
      subidShow = 3;
      break;
    case 'rp4':
      sTitle = '平码四';
      subidShow = 4;
      break;
    case 'rp5':
      sTitle = '特码';
      subidShow = 5;
      break;
    default :  return '-rpn error-'; 
  }
  var showBetSubArr = [
      ['', [rdata.b1, rdata.b2], [1,2], [subidShow, subidShow], showTitleArr, 1, '']
     ,['', [rdata.b4, rdata.b3], [4,3],[subidShow, subidShow], showTitleArr02, 1, '']
     ,['', [rdata.b6], [6], [subidShow], showTitleArr04, 1 , 'four5h0' ]
     ,['', [rdata.b5], [5], [subidShow], showTitleArr05, 1 , 'color5h0' ]
  ];
  sStr += crt_1To21(rdata.b0, 0, subidShow, sTitle, 4, disk, _playtype, xyid);
  sStr += '<tr><th colspan="18" class="bgClc_' + _gametype + '" style="font-family:Arial;font-size:0.95em;">' + sTitle + ' 双面盘</th></tr>';
  for(i = 0, l = showBetSubArr.length; i < l; i++){
    oShow =  showBetSubArr[i];
    sStr += crt_2face_com(oShow[1], oShow[2], oShow[3], oShow[0], oShow[4], oShow[5], 6, disk, _playtype, oShow[6],xyid);
  }
  
  sStr += '</table>';
  return sStr;
}

function show_rxcx(rdata,_lotto_id,_gametype,_playtype, xyid, disk){
  switch(_playtype){
    case 'r1c1': return crt_1To21ChkBox(rdata.b9, 9, 0, '一中一', 3, disk, _playtype); break;
    case 'r2c2': return crt_1To21ChkBox(rdata.b10, 10, 0, '二中二', 3, disk, _playtype); break;
    case 'r3c2': return crt_1To21ChkBox(rdata.b11, 11, 0, '三中二', 3, disk, _playtype); break;
    case 'r3c3': return crt_1To21ChkBox(rdata.b12, 12, 0, '三中三', 3, disk, _playtype); break;
    case 'r4c3': return crt_1To21ChkBox(rdata.b13, 13, 0, '四中三', 3, disk, _playtype); break;
    case 'r5c3': return crt_1To21ChkBox(rdata.b14, 14, 0, '五中三', 3, disk, _playtype); break;
    default: return '-rncn error'; break;
  }
}


function show_setMyNum2(_playtype){
  switch(_playtype){
    case 'rp1': 
    case 'rp2':
    case 'rp3':
    case 'rp4':
    case 'rp5':
      $('#setMyNum2').html(crt_quick02('a', [1,1,1,1,1]) + '<br />');
      break;
    case 'r1c1':
      $('#setMyNum2').html('<span id="B5hTypeSpan01Msg"></span><input type="hidden" value="" name="B5hTypeSpan01" id="B5hTypeSpan01" /><input type="hidden" value="" name="B5hTypeMon" id="B5hTypeMon" />' + crt_quick([1,0],0) +  crt_quick02('a', [1,1,1,1]) + '<br />');
      break;                                                                                                                           
      
    case 'r2c2':
    case 'r3c2':
    case 'r3c3':
    case 'r4c3':
    case 'r5c3':
      $('#setMyNum2').html( '<span id="B5hTypeSpan01Msg"></span>' + crt_quick([1,1],0) + '<span id="quick5hTypeSpana">' + crt_quick02('a', [1,1,1,1]) + '</span><span style="display:none;" id="quick5hTypeSpanb">' + crt_quick02('b', [0,1,1,0,0,1]) + '</span><br /><input type="hidden" value="" name="B5hTypeSpan01" id="B5hTypeSpan01" /><input type="hidden" value="" name="B5hTypeSpan02" id="B5hTypeSpan02" /><input type="hidden" value="" name="B5hTypeMon" id="B5hTypeMon" />' );
      break;                                                                                                                                                                                                                                                                                                                                                                                                          
    default: return 0; break;
  }
  $('<span>快选按钮,四喜与双面(单,双,大,小...)等，<font color="#BE0200">不含号码21</font></span>').appendTo('#setMyNum2');
  $('input.quick5hType').click(function(){
    $('#B5hTypeSpan01Msg').empty();
    var timeBtnType = $("input:hidden#timebtnType").val();
    var attVal1 = '';
    var attVal2 = '';
    switch(timeBtnType){
      case 'text': attVal1 = 'value'; attVal2 = '*'; break;
      case 'checkbox': attVal1 = 'checked'; attVal2 = 'true'; break;
      default: alert('error type'); return false;
    }
    $("#tbdData input:[name^='b_']").clrTheValClr(attVal1);
    var sVal = $(this).val();
    var s2Val = '';
    if(sVal == 'a'){ s2Val = 'b'; _rxcxChkN02 = false; }else{ s2Val = 'a'; _rxcxChkN02 = true;}
    $('#quick5hTypeSpan' + s2Val).hide();
    $('#quick5hTypeSpan' + sVal).show();
  });
  $('#setMyNum2 input:button').click(function(){
    // type
    var timeBtnType = $("input:hidden#timebtnType").val();
    var attVal1 = '';
    var attVal2 = '';
    switch(timeBtnType){
      case 'text': attVal1 = 'value'; attVal2 = '*'; break;
      case 'checkbox': attVal1 = 'checked'; attVal2 = 'true'; break;
      default: alert('error type'); return false;
    }
    var oObj = $("#tbdData input:[name^='b_']");
    var myBtnId = $(this).attr('id').split("_")[1].split("");
    var sNumIdx = 3;
    if(_rxcxChkN02){
      var chkData = $("#tbdData input:[name^='b_']:disabled:checked");
      if(chkData.length != _rxcxChkN){
        alert('请至少勾选' + _rxcxChkN + '胆' );
        return false;
      }
      $("#tbdData input:[name^='b_']:checked:not(:disabled)").clrTheValClr(attVal1);
    }else{
      oObj.clrTheValClr(attVal1);
    }
    switch(myBtnId[0]){
      case 'A':
        var typeCharNum = myBtnId[1] % 2;
        oObj.filter(function(idx, ele){
            var myN = $(ele).attr('name').split("_");
            var sNum = myN[sNumIdx];
            if(sNum == 21){ return false;}
            return (sNum % 2 == typeCharNum);
        }).addTheValClr(attVal1, attVal2);
        break;
      case 'B':
        var typeCharNum = myBtnId[1] -1;
        oObj.filter(function(idx, ele){
            var myN = $(ele).attr('name').split("_");
            var sNum = myN[sNumIdx];
            if(sNum == 21){ return false;}
            if(typeCharNum){ return (sNum < 11); }
            else{ return (sNum > 10); }
        }).addTheValClr(attVal1, attVal2);
        break;
      case 'C':
        var typeCharNum = myBtnId[1] % 2;
        oObj.filter(function(idx, ele){
            var myN = $(ele).attr('name').split("_");
            var sNum = myN[sNumIdx];
            if(sNum == 21){ return false;}
            var sNum_1 = sNum.charAt(0) - 0;
            var sNum_2 = sNum.charAt(1) - 0;
            return (( sNum_1 + sNum_2 ) % 2 == typeCharNum);
        }).addTheValClr(attVal1, attVal2);
        break;
      case 'G':
        var typeCharNum = myBtnId[1] - 0;
        oObj.filter(function(idx, ele){
            var myN = $(ele).attr('name').split("_");
            var sNum = myN[sNumIdx];
            //if(sNum == 21){ return false;}
            return (sNum.charAt(0) == typeCharNum);
        }).addTheValClr(attVal1, attVal2);
        break;
      case 'H':
        var typeCharNum = myBtnId[1] - 0;
        oObj.filter(function(idx, ele){
            var myN = $(ele).attr('name').split("_");
            var sNum = myN[sNumIdx];
            //if(sNum == 21){ return false;}
            return (sNum.charAt(1) == typeCharNum);
        }).addTheValClr(attVal1, attVal2);
        break;
      case 'I':
        var typeCharNum = myBtnId[1];
        if(typeCharNum == 3){ typeCharNum = 0; }
        oObj.filter(function(idx, ele){
            var myN = $(ele).attr('name').split("_");
            var sNum = myN[sNumIdx];
            if(typeCharNum == (sNum % 3)){ return true;}
            else { return false; }
        }).addTheValClr(attVal1, attVal2);
        break;   
      case 'J':
        var typeCharNum = myBtnId[1] - 1;
        oObj.filter(function(idx, ele){
            var myN = $(ele).attr('name').split("_");
            var sNum = myN[sNumIdx];
            var subNum = 0;
            if((sNum % 5) == 0){ subNum = 1;}
            if(typeCharNum == (Math.floor(sNum / 5) - subNum)){ return true;}
            else { return false; }
        }).addTheValClr(attVal1, attVal2);
        break;
      case 'K':
        oObj.addTheValClr(attVal1, attVal2);
        break;  
         
    }
	if(timeBtnType=="checkbox"){
		$("#tbdData input:[name^='b_']:checked").parent().addClass("k1");
		$("#tbdData input:[name^='b_']:not(:checked)").parent().removeClass("k1");
	}
    computeUnion(); // count the numbergruop       
  });
  
  
  
  return 1;
}

$.fn.addTheValClr = function(attVal1, attVal2){
  if(attVal1 == null){ $(this).css('background-color', '#ffff8c'); }
  else{ $(this).css('background-color', '#ffff8c').attr(attVal1, attVal2); }
};

$.fn.clrTheValClr = function(attVal1){
  if(attVal1 != null){ $(this).removeAttr(attVal1); }
  $(this).removeAttr('disabled').css('background-color', '');
  
}

// 连码 - 勾选
function crt_1To21ChkBox(rdata, bettype, subid, sTitle, iNumTr, disk, playgroup){
  var str = "";
  var urlPre = "";
  var setBtnId = "";
  var setBtnName = "";
  var htmlTemp = '<tr class="td2">';
  var htmlTitle = '<th>项目</th><th>赔率</th><th class="bigRight">选择</th>';
  var trArr = new Array();
  var idx = 0;
  var dataLen = rdata.number.length;
  var i = 0;                
  for(i = 0; i < iNumTr; i++){ trArr[i] = '';} 
  var oNum = 0;
  var oRate = 0;              
  var showTitleN = Math.floor(dataLen / iNumTr);
  var numClass = '';
  var numClassN = '';
  var rateClass = "";
  var inpVal = "";
  if((dataLen % iNumTr) > 0){showTitleN += 1; }           
  str += '<caption class="bgClc_' + _gametype   + '">' + sTitle + '</caption>';
  str += '<thead><tr class="td15">';
  for(i = 0; i < showTitleN; i++){ str += htmlTitle; }
  str += '</tr></thead>';
  str += '<tbody align="center" id="tbdData">';
  for(i = 0; i < dataLen; i++){
    idx = i % iNumTr;
    oNum = rdata.number[i];
    oRate = rdata.rates[i];
    urlPre = 'op.php?op=member_5h&fp=bet_fast&gametype=' + _gametype + '&disk=' + disk + '&palygroup=' + playgroup + '&bettype=' + bettype + '&subid=' + subid;
    setBtnId = 'b_' + subid + '_' +  bettype + '_' + oNum;
    setBtnName = 'b_' + subid + '_' +  bettype;
    numClassN = oNum % 3;
    if(numClassN == 0 ){numClassN = 3; }
    numClass = 'color5hB0' + numClassN;
    if( M.Rates[M.rateI] == undefined ) { rateClass = ''; }
    else if( M.Rates[M.rateI] != oRate ) { rateClass = ' rc'; }
    else { rateClass = ''; }
    M.Rates[M.rateI] = oRate;
    M.rateI++;
    // inpVal = $('input[name="' + setBtnId + '"]').val();
    // if( inpVal == undefined ) { inpVal = ''; }    
    trArr[idx] += '<th class="td6 ' + numClass + '">' + oNum + '</th>';
    //trArr[idx] += '<th><a href="' + urlPre + '" class="betRateNum" target="leftFrame">' + oRate + '</a></th>';
    trArr[idx] += '<th class="betRateNum' + rateClass + '">' + oRate + '</th>';
    trArr[idx] += '<td class="bigRight"><input type="checkbox" class="chkNum" name="' + setBtnId + '" value="' + oNum + '" /></td>'; // id="' + setBtnId + '"   
  }
  if(idx++ != iNumTr){
    for(i = idx; i < iNumTr; i++){
      trArr[i] += '<td></td><td></td><td></td>';
    }
  }
  str += htmlTemp + trArr.join('</tr><tr class=td2>') + '</tr>';
  return '<table class="tblMy3D">' + str  + '</table>';
}
                      // rdata, bettype, subid, sTitle, iNumTr, disk, playgroup
function crt_2face_com(rdata, bettype, subBettype, sTitle, showTitleArr, rowSpanN, tdNum, disk,playgroup, bgClass,xyid){
  var setBtnId = "";
  var str = '';
  var strTitle = "";
  var sNum = "";
  var sRate = "";
  var num = 0;
  var setBtnId = "";
  var nextI = -1;        // 可秀玩法
  var i = 0;
  var l = 0;
  var l2 = 0;
  var littletotal = 0;
  var classCount = 0;
  var titleTemp = '';
  var colTemp = '';
  var tempCount = 0;
  var urlPre = '';
  var showClass = '';
  var rateClass = "";
  var inpVal = "";
  str = '<tr class="td2">';
  for(i = 0, l = rdata.length; i < l; i++ ){
    if(i == 0 && sTitle != '') { str += '<th class="td15">' + sTitle + '</th>'; }
    l2 = showTitleArr[i].length;
    for(var j = 0; j < l2; j++){  // 双面
      tempCount++;
      sNum = rdata[i].number[j];
      sRate = rdata[i].rates[j];
      setBtnId = setBtnId = 'b_' + subBettype[i] + '_' +  bettype[i] + '_' + sNum;
      urlPre = 'op.php?op=member_5h&fp=bet_fast&gametype=' + _gametype + '&disk=' + disk + '&palygroup=' + playgroup + '&bettype=' + bettype[i] + '&subid=' + subBettype[i] + '&num=' + sNum+ '&xyid=' + xyid + '&rate=' + sRate;
      if(bgClass != ''){ showClass = ' ' + bgClass + sNum; }else{ showClass = ''; }
      if( M.Rates[M.rateI] == undefined ) { rateClass = ''; }
      else if( M.Rates[M.rateI] != sRate ) { rateClass = ' class="rc"'; }
      else { rateClass = ''; }
      M.Rates[M.rateI] = sRate;
      M.rateI++;
      inpVal = $('input[name="' + setBtnId + '"]').val();
      if( inpVal == undefined ) { inpVal = ''; }
      str += '<th class="title02_3d' + showClass + '" nowrap>' + showTitleArr[i][j] + '</th>';
      str += '<th' +  rateClass+ '><a href="' + urlPre + '" class="betRateNum" target="leftFrame">' + sRate + '</a></th>';
      str += '<td><input type="text" size="3"  value="' + inpVal + '" name="' + setBtnId + '" /></td>';  
    }
  }
  if(tempCount < tdNum){
   for(i = 0; i < (tdNum - tempCount); i++){
    str += '<td></td><td></td><td></td>';
   }
  }
  return str;

}

// n1~n5 - money
function crt_1To21(rdata, bettype, subid, sTitle, iNumTr, disk, playgroup,xyid){
  var str = "";
  var urlPre = "";
  var setBtnId = "";
  var htmlTemp = '<tr class="td2">';
  var htmlTitle = '<th>号码</th><th>赔率</th><th>金额</th>';
  var trArr = new Array();
  var idx = 0;
  var dataLen = rdata.number.length;
  var i = 0;                
  for(i = 0; i < iNumTr; i++){ trArr[i] = '';} 
  var oNum = 0;
  var oRate = 0;              
  var showTitleN = Math.floor(dataLen / iNumTr);
  var numClass = '';
  var numClassN = 0; 
  var rateClass = "";
  var inpVal = "";
  if((dataLen % iNumTr) > 0){showTitleN += 1; }           
  str += '<caption class="bgClc_' + _gametype   + '">' + sTitle + '</caption>';
  str += '<thead><tr class="td15">';
  for(i = 0; i < showTitleN; i++){ str += htmlTitle; }
  str += '</tr></thead>';
  str += '<tbody align=center id="tbdData">';
  for(i = 0; i < dataLen; i++){
    idx = i % iNumTr;
    oNum = rdata.number[i];
    oRate = rdata.rates[i];
    urlPre = 'op.php?op=member_5h&fp=bet_fast&gametype=' + _gametype + '&disk=' + disk + '&palygroup=' + playgroup + '&bettype=' + bettype + '&subid=' + subid + '&num=' + oNum + '&xyid=' + xyid + '&rate=' + oRate;
    setBtnId = 'b_' + subid + '_' +  bettype + '_' + oNum;
    numClassN = oNum % 3;
    if(numClassN == 0 ){numClassN = 3; }
    numClass = 'color5hB0' + numClassN;
    if( M.Rates[M.rateI] == undefined ) { rateClass = ''; }
    else if( M.Rates[M.rateI] != oRate ) { rateClass = ' class="rc"'; }
    else { rateClass = ''; }
    M.Rates[M.rateI] = oRate;
    M.rateI++;
    inpVal = $('input[name="' + setBtnId + '"]').val();
    if( inpVal == undefined ) { inpVal = ''; }
    trArr[idx] += '<th class="title02_3d ' + numClass + '">' + oNum + '</th>';
    trArr[idx] += '<th' +  rateClass+ '><a href="' + urlPre + '" class="betRateNum" target="leftFrame">' + oRate + '</a></th>';
    trArr[idx] += '<td><input type="text" size="3"  value="' + inpVal + '" name="' + setBtnId + '" /></td>'; 
  }
  if(idx++ != iNumTr){
    for(i = idx; i < iNumTr; i++){
      trArr[i] += '<td></td><td></td><td></td>';
    }
  }
  str += htmlTemp + trArr.join('</tr><tr class=td2>') + '</tr>';
  str += '</tbody>';
  return str;
}



// quick button group
function crt_quick(showArr, checkedIdx){
  var str = '';
  var quickArr = ['连碰' , '胆拖'];
  var quickArrL = quickArr.length;
  var quickValArr = ['a' , 'b'];
  var chkStr = '';
  for(var i = 0; i < quickArrL; i++){
    if(i == checkedIdx){chkStr = ' checked';}else{ chkStr = ''; }
    if(showArr[i] > 0){ str += '<label><input type="radio"  class="quick5hType" value="' + quickValArr[i] + '" name="quick5h" ' + chkStr + ' />' + quickArr[i] + '</label>'; }
  }
  return str;
}

function crt_quick02(quick01Id, showArr){
  var str = '';
  var quickArr = ['&nbsp;' , '头', '尾', '色波', '四喜', '快速'];
  var quickArrL = quickArr.length;
  var quickValArr = [['单','双','大','小','合单','合双'] , [' 0 ',' 1 ',' 2 '], [' 0 ',' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ',' 9 '], ['红','蓝','绿'], ['福','禄','寿','喜'],['全选']];
  var quickValIDArr = [['A1','A2','B1','B2','C1','C2'] , ['G0','G1','G2'], ['H0','H1','H2','H3','H4','H5','H6','H7','H8','H9'], ['I1','I2','I3'], ['J1','J2','J3','J4'],['K1']];
  var quickValArrL = 0;
  var chkStr = '';
  var quick01IdStr = 'quick02_' + quick01Id + '_';
  var i = 0;
  var j = 0;
  var oQ = 0;
  var oID = 0;
  var sClass = 'colorBtn01 clrBrd_' + _gametype;
  for(i = 0; i < quickArrL; i++){
    if(showArr[i] > 0){
      if(i == 3){ str += '<br />';}
      str += '&nbsp;&nbsp;<label id="'+ quick01IdStr + i + '">' + quickArr[i];
      oQ = quickValArr[i];
      oID = quickValIDArr[i];
      for(j = 0, quickValArrL = oQ.length; j < quickValArrL; j++ ){
        str += '<input type="button" value="' + oQ[j] + '" id="qckBtn_' + oID[j] + '" class="' + sClass + '" />';
      }
      str += '</label>';
    }
  }
  return str;
}

// -----------------------------------------------------------------------------
// function
// -----------------------------------------------------------------------------

// change money input to fast checkbox button 
$.fn.timebtnTypeToCheckbox = function(){
	mysetcookie("timebtnType","checkbox",1);
  $("input:hidden#timebtnType").val("checkbox");
  $("#myDivTbl input:text[name^='b_']").each(function(){
    //$(this).closest('td').html("<label style='display: block;'><input type=\"checkbox\" value=\"1\" name=\""+$(this).attr("name")+"\" /></label>");
    var o=$(this).closest('td');
	o.html("<input type=\"checkbox\" value=\"1\" name=\"" + $(this).attr("name") + "\" style=\"display:none\" />");
	o.bind("click",function(e){
		var inpu=$(this).find("input");
		if(inpu.attr("checked"))
		{
			$(this).removeClass("k1");	
			inpu.attr("checked",false);
		}
		else
		{
			$(this).addClass("k1");	
			inpu.attr("checked",true);
		}
	});
	return true;
  });
  $("input:button#betByCheckBox").addClass("btn_on");
  $("input:button#betByTextMoney").removeClass("btn_on");
  //$("input:button#betByCheckBox").hide();
  //$("input:button#betByTextMoney").show();
  $("input:button#btnSetMyNum").attr("disabled","true");
};

// chang fast to money input
$.fn.timebtnTypeText = function(){
	mysetcookie("timebtnType","text",1);
  $("input:hidden#timebtnType").val("text");
  $("input:checkbox[name^='b_']").each(function(){
    var o=$(this).closest('td');
	o.html("<input type=\"text\" value=\"\" name=\"" + $(this).attr("name") + "\" size=\"3\" />");
	o.removeClass("k1");
	o.unbind("click");
	return true;
    return true;
  });
  $("input:button#betByTextMoney").addClass("btn_on");
  $("input:button#betByCheckBox").removeClass("btn_on");
  //$("input:button#betByTextMoney").hide();
 // $("input:button#betByCheckBox").show();
  $("input:button#btnSetMyNum").removeAttr("disabled");
};
function leftPar(v)
{
	if(parseInt(v)<=9)
		return "0"+v;
	else
		return v;	
}
var _refstap=30;
$.fn.showNowTime = function(){
  	var thisObj = $(this);
    var str = "";
    if (_timestap >= 0) {
        timediff = _timestap;
    days = Math.floor(timediff / (86400));   // 60 * 60 * 24
  	timediff -= days * (86400);
  	hours = Math.floor(timediff / (3600));  // 60 * 60
  	timediff -= hours * (3600);	
  	mins = Math.floor(timediff / (60));
  	timediff -= mins * (60);	
  	secs = Math.floor(timediff);
    str += "<span style='margin-right:10px'><div style='float:left;width:150px'>距离封盘：";
    if (days > 0 ) str += "<b>" + leftPar(days) + "</b> 天 ";
    if (hours > 0) str += "<b>" + leftPar(hours) + "</b>:";
    if (mins > 0) str += "<b>" + leftPar(mins) + "</b>:";
    str += "<b>" + leftPar(secs) + "</b></div><div style='float:left;width:150px'>距离开奖：";
	//
	timediff2=_timestap+60;
	days = Math.floor(timediff2 / (86400));   // 60 * 60 * 24
  	timediff2 -= days * (86400);
  	hours = Math.floor(timediff2 / (3600));  // 60 * 60
  	timediff2 -= hours * (3600);	
  	mins = Math.floor(timediff2 / (60));
	timediff2 -= mins * (60);	
  	secs = Math.floor(timediff2);
	if (days > 0 ) str += "<b>" + leftPar(days) + "</b> 天 ";
    if (hours > 0) str += "<b>" + leftPar(hours) + "</b>:";
    if (mins > 0) str += "<b>" + leftPar(mins) + "</b>:";
	str += "<b>" + leftPar(secs) + "</b></div><div style='float:left;width:50px;text-align:right;'>"+leftPar(_refstap)+"秒</div></span>";
	_refstap--;
	if(_refstap==0) _refstap=30;
    _timestap-=1;
  }else{
    str += "<font color=red><b>【" + _gametypeStr + "】已经截止下注！</b></font>";
    if ( M.RatesTimeId ) { clearTimeout(M.RatesTimeId); }
    setTimeout('parent.mainFrame.location.reload()',1000);
    setTimeout('parent.leftFrame.location.reload()',1000);
  }
  $(this).html(str);
  _now.setTime(_now.getTime() + 1000);
  setTimeout(function(){$(thisObj).showNowTime();},1000);
};

// 显示讯息方块(取代alert 因alert会使目前时间与截止下注时间暂时停止计算)
$.fn.showMyMsgBox = function(msg){
  $("div#show3dmsg").html("<span class=\"error3d\">"+msg+"</span><br /><input type=\"button\" align=\"center\" id=\"btnOkMsg\" class=\"btnchoice\" value=\"确定\" onclick=\"javascript:$().closeMyMsgBox();\" />");
  $("div#show3dmsg").addClass("show3dmsg").show();
};

$.fn.closeMyMsgBox = function(){
 $("div#show3dmsg").removeClass("show3dmsg").hide();
};

//  更新率赔率禁止使用
$.fn.disAreaObj = function(){
 $("select#xyidSlc").attr("disabled","disabled");
 $("select#slcDisk").attr("disabled","disabled");
 $("input:button#betByCheckBox").attr("disabled","disabled");
 $("input:button#betByTextMoney").attr("disabled","disabled");
};

$.fn.redisAreaObj = function(){
 $("select#xyidSlc").removeAttr("disabled");
 $("select#slcDisk").removeAttr("disabled");
 $("input:button#betByCheckBox").removeAttr("disabled");
 $("input:button#betByTextMoney").removeAttr("disabled");
};

// count group number
function computeUnion(){
 $('#B5hTypeSpan01Msg').empty();
 if(_rxcxChkN > -1){
      var tempSValArr = [];
      var dobuledNum = 0;
      if(_rxcxChkN02){ // 胆
        if($('.tblMy3D input:checkbox:disabled:checked').length < _rxcxChkN){
          $('.tblMy3D input:checkbox:checked').attr('disabled', 'disabled');
          $('.tblMy3D input:checkbox:checked:disabled').each(function(idx, oVal){
            tempSValArr.push($(oVal).val());
            return true;
          });
          $('#B5hTypeSpan01Msg').html( '[<span id="B5hTypeSpan01Span">' + tempSValArr.join(",") + '</span>]<br />');
        }else{
          var temp01 = '';
          var temp02 = '';
          $('.tblMy3D input:checkbox:disabled:checked').each(function(idx, oVal){
            tempSValArr.push($(oVal).val());
            return true;
          });
          temp01 = tempSValArr.join(","); 
          tempSValArr = [];
          dobuledNum = 0;
          $('.tblMy3D input:checkbox:checked:not(:disabled)').each(function(idx, oVal){
            tempSValArr.push($(oVal).val());
            dobuledNum++;
            return true;
          });
          temp02 = tempSValArr.join(","); 
          $('#B5hTypeSpan01Msg').html( '[<span id="B5hTypeSpan01Span">' + temp01 + '</span>][<span id="B5hTypeSpan02Span">' + temp02 + '</span>] 共 ' + dobuledNum + ' 组<br />');
        }
      }else{ // 碰
          $('.tblMy3D input:checkbox:checked').each(function(idx, oVal){
            tempSValArr.push($(oVal).val());
            dobuledNum++;
            return true;
          });
          if(dobuledNum < _rxcxChkN){
            $('#B5hTypeSpan01Msg').html( '[<span id="B5hTypeSpan01Span">' + tempSValArr.join(",") + '</span>]共 0 组<br />');
          }else{
            var betUnionLength = tempSValArr.length; 
            //组合数 
            switch(_rxcxChkN){
              case 0: var bet_Length = betUnionLength; break;
              case 1: var bet_Length = betUnionLength*(betUnionLength-1)/2; break;
              case 2: var bet_Length = betUnionLength*(betUnionLength-1)*(betUnionLength-2)/6;break;
              case 3: var bet_Length = betUnionLength*(betUnionLength-1)*(betUnionLength-2)*(betUnionLength-3)/24;break;
              case 4: var bet_Length = betUnionLength*(betUnionLength-1)*(betUnionLength-2)*(betUnionLength-3)*(betUnionLength-4)/120;break;
              default: alert('error rxcxChkN'); return false; break;
            }
            
            $('#B5hTypeSpan01Msg').html( '[<span id="B5hTypeSpan01Span">' + tempSValArr.join(",") + '</span>]共 ' + bet_Length + ' 组<br />');
          }
      }
  }

}
// -----------------------------------------------------------------------------
$(function(){
  
  // default value for checkbox or money input
  $("input#xyid").attr("value",$("select#xyidSlc option:selected").val());//XY盘类的值
  $("input#disk").attr("value",$("select#slcDisk option:selected").val());
  var chkStr = ',r1c1,r2c2,r3c2,r3c3,r4c3,r5c3,';
  if(chkStr.indexOf(','+ _playtype +',') > -1){
    $("#spanChgBetBtn").hide();
    $("input:hidden#timebtnType").val('checkbox');
    $("#xyidSlc").hide();
  }
  //else if($("input:hidden#timebtnType").val() == "text"){$("input:button#betByTextMoney").hide();}else{$("input:button#betByCheckBox").hide();}
                    
  // time of now
  $("span#showNowTime").showNowTime();
  
  $("div#mainPageData").hide();
  
  // get rate data and show table
  $().getAjaxRatedata(_lotto_id,_gametype,_playtype);
  
  // change to money input
  $("input:button#betByCheckBox").bind('click',function(){
    $().timebtnTypeToCheckbox();
  });
  // change to fast checkbox 
  $("input:button#betByTextMoney").bind('click',function(){$().timebtnTypeText();});
  
  // “transfer”
  $("input:button#btnSetMyNum").bind('click', function(){
    var setMomney = $("input:text#setMomney").val();
    if(setMomney< 1){
      $().showMyMsgBox("请于“金额”右侧之文字方块中输入要转送之金额");
      $("input:text#setMomney").focus();
      return false;
    }else if(setMomney< _betmin){
      $().showMyMsgBox("下注金额不能小于最低下注额("+_betmin+"元)");
      $("input:text#setMomney").focus();
      return false;
    }else{
      $("input:text[name^='b'][value='*']").val(setMomney);
      $("input:text#setMomney").val("");
    } 
  });
  
  // before submit
  $("form").submit(function() {
    if(_rxcxChkN > -1){ // rxcx Special
      if($("input:text#setMomney").val()< 1){
        $().showMyMsgBox("请于“金额”右侧之文字方块中输入下注金额");
        $("input:text#setMomney").focus();
        return false;
      }else if($("input:text#setMomney").val()< _betmin){
        $().showMyMsgBox("下注金额不能小于最低下注额("+_betmin+"元)");
        $("input:text#setMomney").focus();
        return false;
      }else {
        if(_rxcxChkN02){ // 胆
          var B5hTypeSpan01Span = $('#B5hTypeSpan01Span').text();   
          var B5hTypeSpan02Span = $('#B5hTypeSpan02Span').text();
          if(B5hTypeSpan01Span.length < 2){
            $().showMyMsgBox("请至少选择“" + _rxcxChkN + "”胆");
            return false;
          }
          if(B5hTypeSpan02Span.length < 2){
            $().showMyMsgBox("请至少选择 1 托");
            return false;
          }
          if(B5hTypeSpan02Span.split(",").length > 9){
            $().showMyMsgBox("最多只能选择 9 托");
            return false;
          }
          $('#B5hTypeSpan01').val(B5hTypeSpan01Span);
          $('#B5hTypeSpan02').val(B5hTypeSpan02Span);
        }else{
          var B5hTypeSpan01Span = $('#B5hTypeSpan01Span').text();
          var B5hTypeSpan01SpanArr = B5hTypeSpan01Span.split(","); 
          var okrxcxChkN = _rxcxChkN - 0 + 1; 
          if(B5hTypeSpan01SpanArr.length < okrxcxChkN){
            $().showMyMsgBox("请至少选择 " + okrxcxChkN + " 个碰数");
            return false;
          }
          if(B5hTypeSpan01SpanArr.length > 9){
            $().showMyMsgBox("至多选择 9 个碰数");
            return false;
          }
          $('#B5hTypeSpan01').val(B5hTypeSpan01Span);
        }
        $("#B5hTypeMon").val($("input:text#setMomney").val());
        $("#tbdData input[name^='b_']").removeAttr('checked');
      }
    }else{
      switch($("input:hidden#timebtnType").val()){
        case "text": //text
        if($("input:text[name^='b_'][value!='']").not("[value='*']").size() < 1){
          $().showMyMsgBox("请在文字方块中填入要下注的金额；<br />或使用“转送”钮将“*”字转为金额值");
          return false;
        }
        break;
        
        case "checkbox": // checkbox
        if($("input:checkbox[name^='b_']:checked").size() < 1){
          $().showMyMsgBox("请勾选要下注的内容");
          return false;
        }else if($("input:text#setMomney").val()< 1){
          $().showMyMsgBox("请于“金额”右侧之文字方块中输入下注金额");
          $("input:text#setMomney").focus();
          return false;
        }else if($("input:text#setMomney").val()< _betmin){
          $().showMyMsgBox("下注金额不能小于最低下注额("+_betmin+"元)");
          $("input:text#setMomney").focus();
          return false;
        }else {
          $("input:checkbox[name^='b_']:checked").val($("input:text#setMomney").val())
        } 
        break;
      }
    }
    
    $("form input:submit").add("form input:button").attr("disabled","disabled");
    $("input#xyid").attr("value",$("select#xyidSlc option:selected").val());//XY盘类的值
    $("input#disk").attr("value",$("select#slcDisk option:selected").val());
    return true;
  });
  
  $("input:button").hover(
      function() {this.style.color='#0000FF';}
      , function() {this.style.color='';}
  );

  // change rate by XY disk
  $("select#xyidSlc").change(function(){
    $("input#xyid").attr("value",$("select#xyidSlc option:selected").val());
    $().getAjaxRatedata(_lotto_id,_gametype,_playtype);
  });

  // change rate by a,b,c,d disk
  $("select#slcDisk").change(function(){
    if (M.RatesTimeId) { clearTimeout(M.RatesTimeId); }
    $("input#disk").attr("value",$("select#slcDisk option:selected").val());
    M.Rates = new Array();
    $().getAjaxRatedata(_lotto_id,_gametype,_playtype);
  });
  
  // reset button
  $("input#myreset").click(function(){
    var timebtnType = $("input:hidden#timebtnType").val();
    var attVal1 = '';
    switch(timebtnType){
      case 'text': attVal1 = 'value'; break;
      case 'checkbox': attVal1 = 'checked'; break;
      default: alert('error type'); return false;
    }
    var oObj = $("#tbdData input:[name^='b_']");
    oObj.clrTheValClr(attVal1);
    $('input.quick5hType:checked').click();
  });
  
  // show fast bet button
  show_setMyNum2(_playtype);
  
  // show number tbl
  if(_playtype != 'rc' && chgBallArr.length > 0){
    $('.disClc').click(function(){
      $('.onClc').removeClass('onClc');
      $(this).addClass("onClc");  
      var myIDX = $(this).attr("id").substr(5);
      $('#chgBallAreaStr').empty().append(chgBallArr[myIDX]);
      $('#chgBallAreaStr td:even').css("background-color", "#e7ebef");
    });
    $('.disClc:eq(0)').click();
    if(_maxTotal > 0){
      $('#maxDisBall td').each(function(idx, ele){
        if($(ele).text() == _maxTotal){
          $(ele).css({'color':'#FA0300','font-weight':'900'});
        }                                
      });
    }
    if(_maxTotal0 > 0){
      $('#maxDisBall0 td').each(function(idx, ele){
        if($(ele).text() == _maxTotal0){
          $(ele).css({'color':'#FA0300','font-weight':'900'});
        }                                
      });
    }
  }
  
}); 
$(document).ready(function(e) {
    $("#myreset").bind("click",function(){$(".k1").removeClass("k1");});
});